#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 方法1
import requests
from urllib.parse import urljoin

BASE_URL = 'https://login2.scrape.center/'
LOGIN_URL = urljoin(BASE_URL, '/login')
INDEX_URL = urljoin(BASE_URL, '/page/1')
USERNAME = 'admin'
PASSWORD = 'admin'

response_login = requests.post(LOGIN_URL, data={
   'username': USERNAME,
   'password': PASSWORD
}, allow_redirects=False)

cookies = response_login.cookies  # 获取cookies信息
print('Cookies', cookies)

response_index = requests.get(INDEX_URL, cookies=cookies)  # 第二次请求 携带第一次模拟登录获取的Cookies信息
print('Response Status', response_index.status_code)
print('Response URL', response_index.url)

with open('film1.html', 'w', encoding='utf-8') as fp:
    fp.write(response_index.text)  # 将登陆后获取的html页面保存到本地



